<?php
class dealer{
	private $id;
	private $title;
	private $address;
	private	$phone;
	private	$city;
	public $msg = array();
	// set get id
	public function setid($id) {
		$this->id = $id;
	}
	public function getid() {
		return $this->id;
	}
	// set get code
	public function settitle($title) {
		$this->title = $title;
	}
	public function gettitle() {
		return $this->title;
	}
	// set get name
	public function setaddress($address) {
		$this->address = $address;
	}
	public function getaddress() {
		return $this->address;
	}
	// set get name
	public function setphone($phone) {
		$this->phone = $phone;
	}
	public function getphone() {
		return $this->phone;
	}
	public function setcity($city) {
		$this->city = $city;
	}
	public function getcity() {
		return $this->city;
	}
	public function listall() {
		GLOBAL $db;
		$sql = "SELECT * FROM " . $db->tbl_fix . "dealer";
		$result = $db->executeQuery ( $sql );
		while ( $rows = mysql_fetch_array ( $result ) ) {
			$return [] = $rows;
		}
		return $return;
	}
	public function getone() {
		GLOBAL $db;
		$sql = "SELECT * FROM " . $db->tbl_fix . "dealer where id='" . $this->id . "'";
		$return = $db->executeQuery ( $sql, 2 );
		return $return;
	}
	public function listbycity() {
		GLOBAL $db;
		$sql = "SELECT * FROM " . $db->tbl_fix . "dealer where `city` ='".$this->getcity()."' ";
		//echo $sql;
		$result = $db->executeQuery ( $sql );
		while ( $rows = mysql_fetch_array ( $result ) ) {
			$return [] = $rows;
		}
		return $return;
	}
	private function citycount($toantu){
		GLOBAL $db;		
		//print_r($detail);
		if ($toantu == "tru")	$sql = "update " . $db->tbl_fix . "city set numdealer=numdealer-1 where code='".$this->getcity()."'";
		else $sql = "update " . $db->tbl_fix . "city set numdealer=numdealer+1 where code='".$this->getcity()."'";
		$db->executeQuery($sql);
	}
	public function insert(){
		GLOBAL $db;
		if (count($this->msg['error']) == 0){
			//$arr['id'] = $this->getid();
			$arr['title'] = $this->gettitle();
			$arr['address'] = $this->getaddress();		
			$arr['city'] = $this->getcity();
			$arr['phone'] = $this->getphone();		
			$db->record_insert($db->tbl_fix."dealer", $arr);
			$this->citycount("cong");
		} else {
			return $this->msg;
		}		
	}
	
	public function delete() {
		GLOBAL $db;
		$detail = $this->getone();
		$this->setcity($detail["city"]);
		$sql = "DELETE FROM " . $db->tbl_fix . "dealer where `id` ='" . $this->id . "'";
		$db->executeQuery ( $sql );		
		$this->citycount("tru");
	}
	private function updatecity(){
		
	}
}
$dealer = new dealer();
?>